﻿using System;

class Rectangle
{
    // 字段
    private double length;
    private double width;

    // 属性
    public double Length
    {
        get { return length; }
        set
        {
            if (value <= 0)
                throw new ArgumentException("长度必须大于0");
            length = value;
        }
    }

    public double Width
    {
        get { return width; }
        set
        {
            if (value <= 0)
                throw new ArgumentException("宽度必须大于0");
            width = value;
        }
    }

    // 构造函数
    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    // 默认构造函数
    public Rectangle() : this(1, 1) { }

    // 计算周长
    public double GetPerimeter()
    {
        return 2 * (Length + Width);
    }

    // 计算面积
    public double GetArea()
    {
        return Length * Width;
    }

    // 计算对角线长度
    public double GetDiagonal()
    {
        return Math.Sqrt(Length * Length + Width * Width);
    }

    // 判断是否是正方形
    public bool IsSquare()
    {
        return Math.Abs(Length - Width) < double.Epsilon;
    }

    // 重写ToString方法
    public override string ToString()
    {
        return $"长方形[长={Length}, 宽={Width}, 周长={GetPerimeter():F2}, 面积={GetArea():F2}]";
    }

    // 显示详细信息
    public void DisplayInfo()
    {
        Console.WriteLine("长方形信息：");
        Console.WriteLine($"   长度：{Length}");
        Console.WriteLine($"   宽度：{Width}");
        Console.WriteLine($"   周长：{GetPerimeter():F2}");
        Console.WriteLine($"   面积：{GetArea():F2}");
        Console.WriteLine($"   对角线：{GetDiagonal():F2}");
        Console.WriteLine($"   形状：{(IsSquare() ? "正方形" : "长方形")}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("长方形类测试程序");
        Console.WriteLine("================");

        try
        {
            // 测试1：正常长方形
            TestRectangle(5, 3, "测试1：正常长方形");

            // 测试2：正方形
            TestRectangle(4, 4, "测试2：正方形");

            // 测试3：使用用户输入
            TestWithUserInput();

            // 测试4：异常情况
            TestInvalidRectangle();
        }
        catch (Exception ex)
        {
            Console.WriteLine($" 错误：{ex.Message}");
        }
    }

    static void TestRectangle(double length, double width, string testName)
    {
        Console.WriteLine($"\n{testName}");
        Console.WriteLine(new string('-', 30));

        Rectangle rect = new Rectangle(length, width);
        rect.DisplayInfo();

        Console.WriteLine($"ToString(): {rect}");
    }

    static void TestWithUserInput()
    {
        Console.WriteLine("\n 用户输入测试");
        Console.WriteLine(new string('-', 30));

        try
        {
            Console.Write("请输入长方形的长度：");
            double length = double.Parse(Console.ReadLine());

            Console.Write("请输入长方形的宽度：");
            double width = double.Parse(Console.ReadLine());

            Rectangle rect = new Rectangle(length, width);
            rect.DisplayInfo();

            // 额外信息
            Console.WriteLine($"\n 额外信息：");
            Console.WriteLine($"长宽比：{rect.Length / rect.Width:F2}:1");
            Console.WriteLine($"是正方形：{rect.IsSquare()}");
        }
        catch (FormatException)
        {
            Console.WriteLine("输入格式错误！请输入数字。");
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    static void TestInvalidRectangle()
    {
        Console.WriteLine("\n异常情况测试");
        Console.WriteLine(new string('-', 30));

        try
        {
            // 测试负值
            Rectangle invalid = new Rectangle(-5, 3);
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine($"预期错误：{ex.Message}");
        }

        try
        {
            // 测试零值
            Rectangle invalid = new Rectangle(0, 10);
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine($"预期错误：{ex.Message}");
        }
    }
}